package com.yc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.PsychologicalTest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PsychologicalTestDao extends BaseMapper<PsychologicalTest> {
    
    /**
     * 分页查询测评列表，包含用户状态
     */
    @Select("<script>" +
            "SELECT pt.*, tc.name as categoryName, " +
            "COALESCE(utr.status, 'new') as userStatus, " +
            "utr.recordId as recordId " +
            "FROM psychologicalTests pt " +
            "LEFT JOIN testCategories tc ON pt.categoryId = tc.categoryId " +
            "LEFT JOIN userTestRecords utr ON pt.testId = utr.testId AND utr.userId = #{userId} " +
            "WHERE pt.status = 1 " +
            "<if test='categoryId != null and categoryId != \"all\"'>" +
            "AND pt.categoryId = #{categoryId} " +
            "</if>" +
            "<if test='status != null and status != \"all\"'>" +
            "AND COALESCE(utr.status, 'new') = #{status} " +
            "</if>" +
            "ORDER BY pt.sortOrder ASC, pt.createTime DESC" +
            "</script>")
    IPage<PsychologicalTest> selectTestListWithUserStatus(
            Page<?> page, 
            @Param("userId") Long userId, 
            @Param("categoryId") String categoryId, 
            @Param("status") String status
    );
    
    /**
     * 获取测评详情，包含题目和选项
     */
    @Select("SELECT * FROM psychologicalTests WHERE testId = #{testId} AND status = 1")
    PsychologicalTest selectTestDetail(@Param("testId") Long testId);
} 